package cn.lf.android.lawfirm.notice.controller;

import android.content.Context;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import cn.lf.android.lawfirm.AppConfig;
import cn.lf.android.lawfirm.BaseController;
import cn.lf.android.lawfirm.http.manager.EBCallback;
import cn.lf.android.lawfirm.http.manager.MyOkHttpClient;
import cn.lf.android.lawfirm.utils.DebugLog;
import cn.lf.android.lawfirm.utils.SPUtil;

/**
 * Created by qianfan on 2017/9/21.
 */

public class MessageReadController extends BaseController {
    MessageReadCallback mMessageReadCallback;
    private Context context;

    public MessageReadController(MessageReadCallback callback, Context context) {
        this.mMessageReadCallback = callback;
        this.context = context;
    }


    /**
     * @param messageID
     */
    public void onMessageRead(String messageID) {
        Map<String, String> params = new HashMap<String, String>();
        SPUtil spUtil = new SPUtil(context);
        params.put("messageID", messageID);
        DebugLog.e("messageID===" + messageID);

        new MyOkHttpClient().doPost(AppConfig.LSSysMsg_ReadMsg, params,
                new EBCallback() {
                    @Override
                    public void onEBResponse(String response) {
                        DebugLog.e("设置已读===" + response);
                        JSONObject myJsonObject;
                        Boolean isSuc;
                        try {
                            myJsonObject = new JSONObject(response);
                            isSuc = myJsonObject.getBoolean("isSuc");
                            if (isSuc) {
                                mMessageReadCallback.onMessageReadSuccessed("");
                            } else {
                                mMessageReadCallback
                                        .onMessageReadFailed();
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        dismissProgressDia();
                    }

                    @Override
                    public void onEBError(String errorMsg) {
                        DebugLog.e("设置已读失败===" + errorMsg);
                        mMessageReadCallback.onMessageReadFailed();
                        dismissProgressDia();
                    }
                }, "Authorization", spUtil.getAccessToken());
    }


    public interface MessageReadCallback {
        public void onMessageReadSuccessed(String str);

        public void onMessageReadFailed();

    }
}
